package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.guoguojie.biz.MessageBiz;
import cn.sumpu.app.guoguojie.constant.MessageType;
import cn.sumpu.app.guoguojie.dao.MessageDao;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.Message;
import cn.sumpu.app.guoguojie.domain.Ext.MessageExt;
import cn.sumpu.app.guoguojie.domain.Ext.PageModel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.IdGenerator;

public class MessageBizImpl implements MessageBiz {

	private MessageDao messageDao;

	

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	@Override
	public PageModel getMessageFriend(String receiveFamily,Integer page)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("receiveFamily", receiveFamily);
		map.put("type", MessageType.FRIEND.getValue());
		Integer startId = (page - 1) * Common.PAGE_SIZE_MESSAGE;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_MESSAGE);
		List<MessageExt> msgList = messageDao.getMessageFriend(map);
		int count = messageDao.getMessageFriendCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(msgList);
		pm.setPageSize(Common.PAGE_SIZE_MESSAGE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_MESSAGE - 1) / Common.PAGE_SIZE_MESSAGE);
		return pm;
	}
	
	@Override
	public PageModel getMessageInner(String memeber,String receiveFamily,Integer page)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("member", memeber);
		map.put("receiveFamily", receiveFamily);
		map.put("type", MessageType.INNER.getValue());
		Integer startId = (page - 1) * Common.PAGE_SIZE_MESSAGE;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_MESSAGE);
		List<MessageExt> msgList = messageDao.getMessageInner(map);
		int count = messageDao.getMessageInnerCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(msgList);
		pm.setPageSize(Common.PAGE_SIZE_MESSAGE);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_MESSAGE - 1) / Common.PAGE_SIZE_MESSAGE);
		return pm;
	}

	@Override
	public int getMessageNoReadCount(String family) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("receiveFamily", family);
		return messageDao.getMessageNoReadCount(map);
	}

	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public Message insertMessage(Member member,Message message) throws AppBusinessException {
		if(message==null) return null;
		
//		自己家信箱
		message.setIdentifier(IdGenerator.createIdentifier());
		message.setIsRead(true);
		message.setLetterBox(member.getFamily());
		messageDao.insertMessage(message);
//		他家信箱
		message.setIdentifier(IdGenerator.createIdentifier());
		message.setIsRead(false);
		message.setLetterBox(message.getReceiveFamily());
		messageDao.insertMessage(message);
		return message;
	}

	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void deleteMessageById(String id) throws AppBusinessException {
		messageDao.deleteMessageById(id);
		messageDao.deleteMessageByReplyId(id);
	}

	@Override
	public int messageAllNoReadCount(String receiveFamily)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("receiveFamily", receiveFamily);
		return messageDao.getMessageNoReadCount(map);
	}

	@Override
	public int messageFriendNoReadCount(String receiveFamily)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("receiveFamily", receiveFamily);
		map.put("type", MessageType.FRIEND.getValue());
		return messageDao.getMessageNoReadCount(map);
	}

	@Override
	public int messageInnerNoReadCount(String receiveFamily)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("receiveFamily", receiveFamily);
		map.put("type", MessageType.INNER.getValue());
		return messageDao.getMessageNoReadCount(map);
	}

	@Override
	public void readMessageAll(String receiveFamily) throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("receiveFamily", receiveFamily);
		messageDao.updateMessage(map);
	}

	@Override
	public void readMessageFriend(String receiveFamily)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("receiveFamily", receiveFamily);
		map.put("type", MessageType.FRIEND.getValue());
		messageDao.updateMessage(map);
	}

	@Override
	public void readMessageInner(String receiveFamily)
			throws AppBusinessException {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("receiveFamily", receiveFamily);
		map.put("type", MessageType.INNER.getValue());
		messageDao.updateMessage(map);
	}

	@Override
	@Transactional(rollbackForClassName = { "cn.sumpu.app.guoguojie.exception.AppBusinessException" }, timeout = 10)
	public void replyMessage(Member member, String receiveFamily,
			String content, String replyId) throws AppBusinessException {
		Message message = new Message();
		message.setSendMember(member.getIdentifier());
		message.setReceiveFamily(receiveFamily);
		message.setContent(content);
		message.setType(MessageType.INNER.getValue());
		
//		他家的信箱
		message.setIdentifier(IdGenerator.createIdentifier());
		message.setIsRead(false);
		message.setLetterBox(receiveFamily);
		messageDao.insertMessage(message);
//		自己家的信箱
		message.setIdentifier(IdGenerator.createIdentifier());
		message.setIsRead(true);
		message.setLetterBox(member.getFamily());
		message.setReplyMessageId(replyId);
		messageDao.insertMessage(message);
	}
	
	
	

	
}
